﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SEO.Entity;
using System.Data.SqlClient;
using System.Data;

namespace SEO.Business
{
    /// <summary>
    /// 管理员
    /// </summary>
    public class BisAdminInfo
    {
        #region 管理员
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public AdminInfo GetAdmin(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [AdminInfo] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            AdminInfo Info = null;
            while (dr.Read())
            {
                Info = new AdminInfo();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.UserName = dr["UserName"].ToString();
                Info.Name = dr["Name"].ToString();
                Info.UserKey = dr["UserKey"].ToString();
                if (dr["IsLock"] != DBNull.Value)
                    Info.IsLock = Convert.ToBoolean(dr["IsLock"]);
                Info.AdminTypeID = Convert.ToInt32(dr["AdminTypeID"]);
                if (dr["LoginTime"] != DBNull.Value)
                    Info.LoginTime = Convert.ToDateTime(dr["LoginTime"]);
                Info.AddTime = Convert.ToDateTime(dr["AddTime"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 新增管理员信息
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool InsertAdmin(AdminInfo Info)
        {
            string strSql = "insert into [AdminInfo]([UserName],[Passwords],[Name],[AdminTypeID],[UserKey]) values(@UserName,@Passwords,@Name,@AdminTypeID,@UserKey)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@UserName", Info.UserName));
            parms.Add(new SqlParameter("@Passwords", Info.Passwords));
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@AdminTypeID", Info.AdminTypeID));
            parms.Add(new SqlParameter("@UserKey", Info.UserKey));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新管理员信息
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool UpdateAdmin(AdminInfo Info)
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            string strSql = "update [AdminInfo] set [Name]=@Name,[UserKey]=@UserKey{0} where [ID]=@ID";
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@UserKey", Info.UserKey));
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(Info.Passwords))
            {
                strWhere += ",[Passwords]=@Passwords";
                parms.Add(new SqlParameter("@Passwords", Info.Passwords));
            }
            if (Info.AdminTypeID != null)
            {
                strWhere += ",[AdminTypeID]=@AdminTypeID";
                parms.Add(new SqlParameter("@AdminTypeID", Info.AdminTypeID));
            }
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, string.Format(strSql,strWhere), parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新管理员信息
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool UpdatePasswords(AdminInfo Info)
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            string strSql = "update [AdminInfo] set [Passwords]=@Passwords where [ID]=@ID";
            parms.Add(new SqlParameter("@Passwords", Info.Passwords));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 获取指定用户名的管理员信息
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public DataTable GetAdminInfo(string strUserName)
        {
            return SqlHelper.ExecuteDataset(
                      SqlHelper.database,
                      CommandType.Text,
                      "select top 1 a.[ID],[UserName],[Passwords],a.[Name],[IsLock],[AdminTypeID],[LoginTime],t.[Features] from [AdminInfo] a left join [AdminType] t on a.[AdminTypeID]=t.[ID] where [UserName]=@UserName",
                      new SqlParameter("@UserName", strUserName)).Tables[0];
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllAdminList()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[UserName],[Name] from [AdminInfo]").Tables[0];
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUserList()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[UserName],[Name] from [AdminInfo] where [ID]<>1 order by [ID]").Tables[0];
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllUserByLock()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[UserName],[Name] from [AdminInfo] where [ID]<>1 and [IsLock]=0 order by [ID]").Tables[0];
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllListByKey()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select [ID],[UserKey] from [AdminInfo] where [UserKey]<>'' and [UserKey] is not null order by [ID]").Tables[0];
        }

        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <param name="nWebSiteID">站点ID</param>
        /// <param name="nSortID">分类ID</param>
        /// <param name="eType">类型</param>
        /// <param name="strName">标题</param>
        /// <param name="nPageIndex">当前页码</param>
        /// <param name="nPageSize">分页容量</param>
        /// <returns></returns>
        public DataTable GetAdminList(string strUserName, string strName, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[6];
            parms[0] = new SqlParameter("@UserName", strUserName);
            parms[1] = new SqlParameter("@Name", strName);
            parms[2] = new SqlParameter("@PageIndex", nPageIndex);
            parms[3] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[4] = new SqlParameter("@PageCount", nPageCount);
            parms[4].Direction = ParameterDirection.Output;
            parms[5] = new SqlParameter("@RecordCount", nRecordCount);
            parms[5].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetAdminList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[4].Value);
            nRecordCount = Convert.ToInt32(parms[5].Value);
            return dt;
        }

        /// <summary>
        /// 获取管理员信息列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAdminList()
        {
            return SqlHelper.ExecuteDataset(
                      SqlHelper.database,
                      CommandType.Text,
                      "select * from [AdminInfo]").Tables[0];
        }


        /// <summary>
        /// 更新管理员登陆日期
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool SetLoginTime(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                    SqlHelper.database,
                    CommandType.Text,
                    "update [AdminInfo] set [LoginTime]=getdate() where [ID]=@ID",
                    new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 更新管理员状态
        /// </summary>
        /// <param name="nID">ID</param>
        /// <param name="bIsLock">(false:开通,true:锁定)</param>
        /// <returns></returns>
        public bool SetLock(int nID, bool bIsLock)
        {
            string strSql = "update [AdminInfo] set [IsLock]=@IsLock where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@IsLock", bIsLock));
            parms.Add(new SqlParameter("@ID", nID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.StoredProcedure,
                   "DeleteAdmin",
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 验证管理员用户名是否存在
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public bool CheckUserName(string strUserName)
        {
            object retVal = SqlHelper.ExecuteScalar(
                                SqlHelper.database,
                                CommandType.Text,
                                "select top 1 [ID] from [AdminInfo] where [UserName]=@UserName",
                                new SqlParameter("@UserName", strUserName));
            return retVal == null ? false : true;
        }

        /// <summary>
        /// 验证识别码是否存在
        /// </summary>
        /// <param name="strUserKey"></param>
        /// <returns></returns>
        public bool CheckUserKey(int nID, string strUserKey)
        {
            object retVal = SqlHelper.ExecuteScalar(
                              SqlHelper.database,
                              CommandType.Text,
                              "select top 1 [ID] from [AdminInfo] where [UserKey]=@UserKey and (@ID=0 or [ID]<>@ID)",
                              new SqlParameter("@UserKey", strUserKey),
                              new SqlParameter("@ID", nID));
            return retVal == null ? false : true;
        }
        #endregion

        #region 管理员类型
        /// <summary>
        /// 获取管理员类型
        /// </summary>
        /// <param name="nID"></param>
        /// <returns></returns>
        public AdminType GetAdminType(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [AdminType] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            AdminType Info = null;
            while (dr.Read())
            {
                Info = new AdminType();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.Name = dr["Name"].ToString();
                Info.Features = (long)(dr["Features"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 获取权限值列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAdminFeatures()
        {
            return SqlHelper.ExecuteDataset(
                     SqlHelper.database,
                     CommandType.Text,
                     "select * from [AdminFeatures] order by [ParentID],[OrderNum]").Tables[0];
        }

        /// <summary>
        /// 获取管理员权限组列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAdminTypeList()
        {
            return SqlHelper.ExecuteDataset(
                    SqlHelper.database,
                    CommandType.Text,
                    "select * from [AdminType] order by [ID] desc").Tables[0];
        }

        /// <summary>
        /// 新增管理员类型
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool InsertAdimType(AdminType Info)
        {
            string strSql = "insert into [AdminType]([Name],[Features]) values(@Name,@Features)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Features", Info.Features));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 编辑管理员类型
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool UpdateAdimType(AdminType Info)
        {
            string strSql = "update [AdminType] set [Name]=@Name,[Features]=@Features where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Name", Info.Name));
            parms.Add(new SqlParameter("@Features", Info.Features));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除管理员类型
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool DeleteAdimType(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.StoredProcedure,
                   "DeleteAdminType",
                   new SqlParameter("@ID", nID)) > 0;
        }

        #endregion
    }
}
